package com.algorithm.code.hash;

import java.util.*;

/**
 * @program: study
 * @className: LRUBaseHashTable
 * @description: TODO
 * @author: kukuxiahuni
 * @create: 2020-02-07 17:50
 **/
public class LRUBaseHashTable<K, V> implements LRUAlgorithm<K, V> {

    /**
     * LRU默认最大容量
     */
    private static final int LRU_CAPACITY = 5;

    /**
     * 伪头结点
     */
    private Node<K, V> head;
    /**
     * 伪尾节点
     */
    private Node<K, V> tail;

    private final HashMap<K, Node<K, V>> table;

    public LRUBaseHashTable(int capacity) {
        this.length = 0;
        this.capacity = capacity;
        this.head = new Node<>(null, null);
        this.tail = new Node<>(null, null);

        head.next = tail;
        tail.pre = head;

        this.table = new HashMap<>();
    }

    /**
     * @method: 构造函数
     * @param:
     * @return:
     * @author: kukuxiahuni
     * @create: 2020/2/7 18:41
     * @modify_user:TODO
     * @modify_time: 2020/2/7 18:41
     **/
    public LRUBaseHashTable() {
        this(LRU_CAPACITY);
    }

    /**
     * 链表长度
     */
    private int length;
    /**
     * 链表容量
     */
    private int capacity;

    /**
     * @method: 双向链表节点
     * @param:
     * @return:
     * @author: kukuxiahuni
     * @create: 2020/2/7 17:54
     * @modify_user:TODO
     * @modify_time: 2020/2/7 17:54
     **/
    private final static class Node<K, V> {
        K key;
        V value;
        Node<K, V> pre;
        Node<K, V> next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }


    @Override
    public void add(K key, V value) {

        Node<K, V> node = this.table.get(key);

        if (node == null) {
            Node<K, V> newNode = new Node<>(key, value);
            table.put(key, newNode);
            //将节点加入到LRU链表
            this.addNode(newNode);
            ++length;
            //判断是否超出链表长度
            if (this.length > this.capacity) {
                //需要删除尾部节点
                Node<K, V> deleteNode = this.popTail();
                this.table.remove(deleteNode.key);
                --length;
            }

        } else {
            node.value = value;
            /**
             * 将该元素移动到头部
             */
            moveToHead(node);
        }

    }

    @Override
    public void delete(K key) {

        if (key == null) {
            return;
        }

        Node<K, V> node = this.table.get(key);
        if (node == null) {
            return;
        }

        remove(node);
        --length;
        this.table.remove(node.key);

    }

    @Override
    public V get(K key) {
        if (key == null) {
            return null;
        }
        Node<K, V> node = this.table.get(key);
        if (node == null) {
            return null;
        }
        moveToHead(node);
        return node.value;
    }

    /**
     * 向头部加入节点
     *
     * @param node
     */
    private final void addNode(Node<K, V> node) {
        node.next = head.next;
        head.next.pre = node;
        head.next = node;
        node.pre = head;
    }

    /**
     * @method: 删除尾部节点
     * @param:
     * @return:
     * @author: kukuxiahuni
     * @create: 2020/2/7 18:55
     * @modify_user:TODO
     * @modify_time: 2020/2/7 18:55
     **/
    private final Node<K, V> popTail() {

        Node<K, V> node = tail.pre;

        remove(node);

        return node;

    }

    /**
     * @method: 移除节点
     * @param:
     * @return:
     * @author: kukuxiahuni
     * @create: 2020/2/7 19:01
     * @modify_user:TODO
     * @modify_time: 2020/2/7 19:01
     **/
    private final void remove(Node<K, V> node) {
        node.pre.next = node.next;
        node.next.pre = node.pre;
    }

    /**
     * @param node
     */
    private final void moveToHead(Node<K, V> node) {
        remove(node);
        addNode(node);


    }

    public static void main(String[] args) {
//        LRUBaseHashTable<Integer, String> cache = new LRUBaseHashTable<>(4);
//        for (int i = 0; i < 6; ++i) {
//            cache.add(i, "i" + i);
//        }
//
//        cache.get(3);
//        cache.get(4);

        StringBuilder sb = new StringBuilder();
        sb.insert(0, 'c');
        sb.insert(0, 'a');
        System.out.println(sb.toString());
        sb.deleteCharAt(sb.length() - 1);


        Deque<String> pathStack = new LinkedList<>();
        pathStack.push(String.valueOf(1));


    }

}
